package com.xiaochong.bitong.api.webWs;

import com.xc.logclient.utils.LogTrace;

import javax.websocket.Session;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.PriorityBlockingQueue;

/**
 * @Project : xc-tir
 * @Package Name : com.xiaochong.tir.api.webws
 * @Description : TODO WS连接管理容器
 * @Author : daizhiqing@xiaochong.com
 * @Creation Date : 2018年05月28日下午7:56
 * @ModificationHistory Who When What
 */
public class WsSessionContainer {

    private static ConcurrentHashMap<String, Session> sessionMap = new ConcurrentHashMap<>();
    private static ConcurrentHashMap<String, Long> sessionDatetimeMap = new ConcurrentHashMap<>();//存放当前session最新请求时间，超过30秒自动关闭
    public static ConcurrentHashMap<String, Session> getSessionMap() {
        return sessionMap;
    }


    /*public static void addSession(Session session,int exchangeId,String transactionName){
        if(session != null){
            sessionMap.put(session.getId()+":"+exchangeId+":"+transactionName, session);
        }
    }*/
    public static void addSession(Session session,String wsClass,String type,long datetime){
        if(session != null){
            sessionMap.put(session.getId()+":"+wsClass+":"+type, session);
            addSessionDatetime(session,datetime);
        }
    }
    public static Session removeSession(Session session,String wsClass,String type){
        if(session != null){
            removeSessionDatetime(session);
            return sessionMap.remove(session.getId()+":"+wsClass+":"+type);
        }
        return null;
    }

    /**
     * 移除session的数据
     * @param session
     */
    public static void removeSessionDatetime(Session session){
        if(session != null){
            sessionDatetimeMap.remove(session.getId());
        }
    }
    /**
     * 加入session的数据
     * @param session
     */
    public static void addSessionDatetime(Session session,long datetime){
        if(session != null){
            sessionDatetimeMap.put(session.getId(),datetime);
        }
    }
    /**
     * 定时清理session
     * @param session
     * @param sessionId
     * @return
     */
    /*public static Session removeSession(Session session,String sessionId){
        if(session != null){
            return sessionMap.remove(sessionId);
        }
        return null;
    }*/
    /**
     * 群发
     * @param msg
     * @return
     */
    public static int sendAllMsg(String msg ,int exchangeId,String transactionName){
        LogTrace.info("sendAllMsg","key",exchangeId+transactionName+"");
        int count = 0;
        for (String key: sessionMap.keySet()) {
            if(key.endsWith(exchangeId+transactionName+"")){
                try {
                    synchronized(key) {
                        sessionMap.get(key).getBasicRemote().sendText(msg);
                    }
                }catch (Exception e){
                    e.printStackTrace();
//                    continue;
                }
                count ++;
            }
        }
        return count;
    }

    public static ConcurrentHashMap<String, Long> getSessionDatetimeMap() {
        return sessionDatetimeMap;
    }
}
